##################################################################
# /* Author: Gautam Nair Nicholas Sambanis */
# /* Project: Nationalism Ethnic Identification */
# /* Description: Graphical Presentation of Heterogeneous Effects of Violence Exposure by Constituency National Identification */

##################################################################
# loading packages
##################################################################
library(Hmisc)
library(foreign)
library(sandwich)
library(lmtest)
library(numDeriv)
library(stargazer)
library(ggplot2)
#library(plyr)
library(gridExtra)
library(dplyr)
library(scales)

##################################################################
# Graphical CATE by Above/Below Median National Identification Constituency
##################################################################

rm(list=ls())
data.working <- read.csv("nei_kashmir_replication_dataset.csv")

data.working <- as.data.frame(data.working[data.working$group1_control==1 | data.working$group2_violence==1,])

# Plot 1

temp.font <- 9
temp.legend <-8
treatvar <- c("group2_violence")
xvar <- c("ic_high_national_id_acc")
temp.x.label=c("Control","Violence")

changing.var <- c("identity_rank_indian_rev", "identity_choice_indian_both", "bonus_donation_india", "kashmir_status_india", "ind_pak_prefer_ind", "protest_peaceful_endorse" ,"protest_violent_endorse")

temp.title <-  c("Ranked Indian Identity (Reversed 1-4)", "Indian Indian+Kashmiri/Kashmiri Only (1/0)", "Donation to All-India NGO (%)", "Kashmir remain Part of India (1/0)", "India/Pakistan: Prefer India (%)", "Peaceful Protest Participation (1-5)", "Violent Protest Endorsement (1-5)")

file.name <-  c("tf_f_09_01_cate_violence_cons_identity_rank.png", 
"tf_f_09_02_cate_violence_cons_identity_choice.png", 
"tf_f_09_03_cate_violence_cons_bonus_donation.png", 
"tf_f_09_04_cate_violence_cons_kashmir_status.png", 
"tf_f_09_05_cate_violence_cons_india_pakistan_prefer_ind.png", 
"tf_f_09_06_cate_violence_cons_protest_peaceful_endorse.png", 
"tf_f_09_07_cate_violence_cons_protest_violent_endorse.png")

numdigits <- c(2,2,1,2,2,2,2)


for(i in 1: length(changing.var)){
	myvars <- c(treatvar, xvar, changing.var[i])
	data.temp <- data.working[,myvars]
	data.temp <- na.omit(data.temp)
	D <- data.temp[,1]
	X <- data.temp[,2]
	Y <- data.temp[,3]
	outcome <- cbind(D,X,Y)
	outcome <- as.data.frame(outcome)
	outcome <- group_by(outcome, D, X)
	outcome <- summarize(outcome, 
	count=n(),
	y_bar=mean(Y, na.rm=TRUE),
	y_bar_100=format(round((mean(Y, na.rm=TRUE)), digits=numdigits[i]),nsmall=1),
	se=sd(Y, na.rm=TRUE)/sqrt(n()),
	upper95 = y_bar + 1.96*se,
	lower95= y_bar - 1.96*se,
	upper90 = y_bar + 1.65*se,
	lower90= y_bar - 1.65*se)
	outcome$X[outcome$X==0] <- 10
	outcome$X[outcome$X==1] <- 20
	outcome$X <- as.factor(outcome$X)
	outcome$D <- as.factor(outcome$D)
	outcome$aux <- c(9,11,14,16)

	temp.plot <- ggplot(outcome, aes( x=aux, y = y_bar, ymin = lower95, ymax = upper95, label=y_bar_100)) +
	geom_linerange(aes(color = X), size = 0.7, alpha = 0.5) +
	geom_point(aes(color = X, shape = X), size=3.0) + 
	#geom_text(size=3.5, hjust=-0.3) +
	geom_line(aes(color = X), size=0.1) +
	xlab("") + ylab("") +
	scale_x_continuous(name="", breaks=c(10,15), labels=temp.x.label, limits=c(7,18)) + # here you define coordinates for A and B 
	theme_bw(base_size = temp.font) +
	theme(panel.grid.minor.x=element_blank(),panel.grid.major.x=element_blank(), panel.grid.minor.y=element_blank(), panel.grid.major.y=element_blank(), legend.title=element_blank(), plot.title = element_text(size = temp.font), panel.grid.minor= element_blank(), axis.text=element_text(size=temp.font), axis.title=element_text(size=temp.font), legend.position = "none",legend.margin = unit(0.01, "cm"), legend.text = element_text(size=temp.legend), legend.key = element_blank()
	) +
	ggtitle(temp.title[i]) 
	ggsave(temp.plot, filename=file.name[i], width=3,height=3)
	temp.plot
}



